from typing import Type, List

from db import models
from domain.domain import UpdateVO, VersionVO, MessageVO


async def Version2UpdateVOWrapper(a: models.Version) -> UpdateVO:
    version_str = await int2version(a.version)
    b = UpdateVO()
    b.title = "更新提示"
    b.update_tips = "您目前的版本可更新至" + version_str
    b.md5 = a.md5
    b.update_version_code = await int2version(a.version)
    b.download_url = a.download_url
    return b


async def message2messageVOWrapper(message: models.VersionMessage) -> MessageVO:
    message_vo = MessageVO()
    message_vo.version = await int2version(message.version)
    message_vo.bid = message.bid
    message_vo.user_bid = message.user_bid
    message_vo.create_time = message.create_time
    message_vo.version_bid = message.version_bid
    message_vo.content = message.content
    message_vo.operation = {1: "创建", 2: "修改", 3: "删除", 4: "上线", 5: "下线"}.get(message.operation)
    return message_vo


async def version2VersionVOList(i: models.Version) -> VersionVO:
    one_vo = VersionVO()
    one_vo.version = await int2version(i.version)
    one_vo.bid = i.bid
    one_vo.download_url = i.download_url
    one_vo.max_version = await int2version(i.max_version)
    one_vo.min_version = await int2version(i.min_version)
    one_vo.is_delete = i.is_delete
    one_vo.is_release = i.is_release
    one_vo.platform = i.platform
    one_vo.channel = i.channel
    one_vo.max_os_api = i.max_os_api
    one_vo.min_os_api = i.min_os_api
    one_vo.cpu_arch = i.cpu_arch
    one_vo.aid = i.aid
    one_vo.pop_count = i.pop_count
    one_vo.install_count = i.install_count
    one_vo.create_time = i.create_time
    one_vo.delete_time = i.delete_time
    return one_vo


async def version2int(version) -> int:
    version = version.split(".")
    ret = 0
    for i in version:
        ret = ret * (10 ** 2) + int(i)
    return ret


async def int2version(_int):
    s = []
    while _int:
        s.append(str(_int % (10 ** 2)))
        _int = _int // (10 ** 2)
    s.reverse()
    return ".".join(s)
